package test.test_7_4;
/*
分别定义以下类：
 教师类Teacher
 属性：int age;String name;Student stu
 学生类Student
 属性：int age；String name；Star s
 明星类Star
 属性：int age，String name
 尝试完成Teacher对象的深度克隆，并写代码进行测试
 */
public class Demo02 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Teacher teacher = new Teacher("张三",25,new Student("wzx",25,new Start()));
        Teacher teacherClone = teacher.clone();
        System.out.println(teacher);
        System.out.println(teacherClone);
        System.out.println(teacher == teacherClone);

    }
}
class Teacher implements Cloneable {
    String name;
    int age;
    Student student;

    public Teacher() {
    }

    public Teacher(String name, int age, Student student) {
        this.name = name;
        this.age = age;
        this.student = student;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", student=" + student +
                '}';
    }

    @Override
    protected Teacher clone() throws CloneNotSupportedException {
        Teacher teacher = (Teacher) super.clone();
        //student克隆一份
        Student studentClone = teacher.student.clone();
        //把克隆体指向student
        teacher.student = studentClone;
        return teacher;
    }
    //重写clone方法
}


class Student implements Cloneable{
    String name;
    int age;
    Start start;

    public Student() {
    }

    public Student(String name, int age, Start start) {
        this.name = name;
        this.age = age;
        this.start = start;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", start=" + start +
                '}';
    }


    @Override
    public Student clone() throws CloneNotSupportedException{
        Student student = (Student) super.clone();
        //Start克隆一份
        Start startClone = student.start.clone();
        //修改克隆体中的Start的指向
        student.start = startClone;
        return student;

    }
}
class Start implements Cloneable{
    int age;
    String name;


    @Override
    public Start clone() {
        try {
            Start clone = (Start) super.clone();
            return clone;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }
}